package com.tanxi521.commandmanager;

import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class BugMCommand implements CommandExecutor {

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (!sender.isOp()) return false;
        if (args.length <= 0) return false;
        if (args[0].equalsIgnoreCase("get")) {
            if (sender instanceof Player) {
                Player player = (Player) sender;
                ItemStack item = player.getInventory().getItemInMainHand();
                String name = item.getType().name();
                TextComponent textComponent = new TextComponent(name);
                ClickEvent clickEvent = new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, name);
                textComponent.setClickEvent(clickEvent);
                player.spigot().sendMessage(textComponent);
                return true;
            }
        }
        if (args.length == 2 && args[0].equals("test")) {
            String arg = args[1];
            String s = arg.replaceAll("&.+?", "");
            CommandManager.getInstance().getLogger().info(s);
            CommandManager.getInstance().getLogger().info("长度为" + s.length());
        }

        if (args[0].equalsIgnoreCase("findBlock")) {
            Bukkit.getScheduler().runTaskAsynchronously(CommandManager.getInstance(), () -> findBlock(args));
            return true;
        }

        return false;
    }


    private void findBlock(String[] args) {
        String worldName = args[1];
        String type = args[2];
        String name = args[3];
        World world = Bukkit.getWorld(worldName);
        Chunk[] loadedChunks = world.getLoadedChunks();

        List<String> loc = new ArrayList<>();

        for (Chunk chunk : loadedChunks) {

            int chunkX = chunk.getX(); // 区块的X坐标
            int chunkZ = chunk.getZ(); // 区块的Z坐标

            for (int x = 0; x < 16; x++) {
                for (int y = 0; y < world.getMaxHeight(); y++) {
                    for (int z = 0; z < 16; z++) {
                        Block block = chunk.getBlock(x, y, z);
                        String blockName = block.getType().name();
                        if (blockName.equals(type)) {
                            // 将区块内坐标转换为世界坐标
                            int worldX = chunkX * 16 + x;
                            int worldZ = chunkZ * 16 + z;
                            loc.add("坐标 " + worldX + "," + y + "," + worldZ + " 处存在一个 " + name);
                        }
                    }
                }
            }

        }
        writeToFile(loc, name);
    }

    private void writeToFile(List<String> lines, String name) {
        // 将字符串列表写入文件
        try {
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");
            String fileName = "blockLog" + File.separator + name + sdf.format(date) + ".txt";
            File file = new File(CommandManager.getInstance().getDataFolder(), fileName);

            if (!file.exists()) if (!file.createNewFile()) throw new RuntimeException("文件创建失败");

            // 使用FileWriter写入内容
            try (FileWriter writer = new FileWriter(file)) {
                for (String line : lines) {
                    writer.write(line + System.lineSeparator());
                }
            }
            CommandManager.getInstance().getLogger().info(fileName + " 记录写入完成, 请查看blockLog");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
